package com.qf.ui;

import javax.swing.*;
import java.awt.*;

public class MenuFrame extends JFrame {

    public MenuFrame(){
        //调用菜单页面的初始化方法
        init();
    }

    public void init(){
        //设置标题
        this.setTitle("在线考试");
        //设置尺寸
        this.setSize(600,400);
        //设置居中显示
        this.setLocationRelativeTo(null);
        //添加面板
        this.setContentPane(createContentPane());
    }
    //定义菜单页面 面板创建方法
    private JPanel createContentPane() {
        JPanel p=new JPanel(new BorderLayout());
        //导入图片
        ImageIcon icon=new ImageIcon(this.getClass().getResource("title.png"));

        p.add(BorderLayout.NORTH,new JLabel(icon));
        p.add(BorderLayout.CENTER,createMenuPane());
        p.add(BorderLayout.SOUTH,new JLabel("版权所有 盗版必究",JLabel.RIGHT));
        return p;
    }
    //定义中部画板
    private JPanel createMenuPane() {
        JPanel p=new JPanel(new BorderLayout());
        p.add(BorderLayout.NORTH,new JLabel("XXX 同学 你好",JLabel.CENTER));
        p.add(BorderLayout.CENTER,createBtnPane());
        return p;
    }
    //定义按钮创建方法
    private JPanel createBtnPane(){
        JPanel p=new JPanel(new FlowLayout());
        JButton start=createImageBtn("exam.png","开始");
        JButton result=createImageBtn("result.png","考试成绩");
        JButton msg=createImageBtn("message.png","考试信息");
        JButton exit=createImageBtn("exit.png","离开");
        p.add(start);
        p.add(result);
        p.add(msg);
        p.add(exit);
        return p;
    }

    //创建按键方法
    private JButton createImageBtn(String img,String txt){
        ImageIcon icon=new ImageIcon(this.getClass().getResource(img));
        JButton button=new JButton(txt,icon);
        //垂直方向的调整
        button.setVerticalTextPosition(JButton.BOTTOM);
        //水平方向的调整
        button.setHorizontalTextPosition(JButton.CENTER);
        return button;
    }
}
